package com.xyazm.coupon.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyazm.common.api.CommonPage;
import com.xyazm.common.api.CommonVO;
import com.xyazm.coupon.domain.param.resource.AddCouponResourceParam;
import com.xyazm.coupon.domain.param.resource.ListCouponResourceParam;
import com.xyazm.coupon.domain.param.resource.UpdateCouponResourceParam;
import com.xyazm.coupon.domain.vo.CouponResourceVO;
import com.xyazm.coupon.entity.CouponResourceDO;
import com.xyazm.coupon.mapstruct.CouponResourceMapstruct;
import com.xyazm.coupon.service.ICouponResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  优惠券资源
 * </p>
 *
 * @author xuyou
 * @since 2021-08-09
 */
@RestController
@RequestMapping("/coupon/couponResource")
public class CouponResourceController {


    @Autowired
    private ICouponResourceService couponResourceService;


    /**
     * 获取优惠券资源列表
     */
    @GetMapping("/list")
    public CommonVO<CommonPage> list(ListCouponResourceParam param){
        Page<CouponResourceDO> page = couponResourceService.list(param);
        List<CouponResourceVO> vos = CouponResourceMapstruct.INSTANCE.couponResourceDTOToVO(page.getRecords());
        return CommonVO.success(CommonPage.convertPage(page.getTotal(),vos));
    }


    /**
     * 根据优惠券批次号查询券资源详情
     */
    @GetMapping(value = "/get")
    public CouponResourceVO get(@RequestParam(value = "resourceBatchId")Long resourceBatchId) {
        CouponResourceDO couponResource = couponResourceService.getById(resourceBatchId);
        return CouponResourceMapstruct.INSTANCE.couponResourceDTOToVO(couponResource);
    }


    /**
     * 新增优惠券资源
     */
    @PostMapping(value = "/add")
    public Long add(@RequestBody AddCouponResourceParam param) {
        Long resourceBatchId = couponResourceService.add(param);
        return resourceBatchId;
    }

    /**
     * 更新优惠券资源
     */
    @PostMapping(value = "/update")
    public void update(@RequestBody UpdateCouponResourceParam param) {
        couponResourceService.update(param);
    }


}
